﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XProject.DataVisit.Model;
using System.Web;
using XProject.DataVisit.Factory;
using XProject.DataVisit.Model.Advertisement;
using System.IO;
using XProject.DataVisit.BLL;
using XProject.DataVisit.Model.Temp;
using XProject.DataVisit.Model.Flow;
using XProject.DataVisit.Model.Doc;

namespace XProject.DataVisit.Handler.View.Advertisement
{
    public class AdvertisementView:AbstractView
    {
        private UserInfo CurrentUser;
        public override void init()
        {
            if (HttpContext.Current.Session["UserInfo"] != null)
            {
                CurrentUser = HttpContext.Current.Session["UserInfo"] as UserInfo;
                base.init();
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 获取通过审核广告
        /// </summary>
        public void GetAdvertisement()
        {
            SearchAd sa = new SearchAd();
            sa.SearchType = 1;
            JsonResponse.ResponseObject = DaoFactory.AdvertisementDao.GetSearchList(sa);
        }

        /// <summary>
        /// 获取我发布的广告
        /// </summary>
        public void GetMyAdvertisement()
        {
            var info = JsonRequest.GetParam<SearchAd>("info");
            JsonResponse.ResponseObject = DaoFactory.AdvertisementDao.GetMyList(CurrentUser,info);
        }

        /// <summary>
        /// 查询广告管理
        /// </summary>
        public void GetsearchAdvertisement()
        {
            var info = JsonRequest.GetParam<SearchAd>("info");
            JsonResponse.ResponseObject = DaoFactory.AdvertisementDao.GetSearchList(info);
        }

        public void SaveBindAd_Equipment()
        {
            var ftime = JsonRequest.GetParam<DateTime>("ftime");
            var fh = JsonRequest.GetParam<string>("fh");
            var fm = JsonRequest.GetParam<string>("fm");
            var fs = JsonRequest.GetParam<string>("fs");
            var etime = JsonRequest.GetParam<DateTime>("etime");
            var eh = JsonRequest.GetParam<string>("eh");
            var em = JsonRequest.GetParam<string>("em");
            var es = JsonRequest.GetParam<string>("es");
            var ad = JsonRequest.GetParam<Guid>("ad");
            var ID = JsonRequest.GetParam<Guid>("ID");

            BindAdvertisement ba = new BindAdvertisement();
            ba.AdvertisementID = ad;
            ba.Creater = CurrentUser.UserAccount;
            ba.EquipmentID = ID;
            ba.ETime = new DateTime(etime.Year, etime.Month, etime.Day, Convert.ToInt32(eh), Convert.ToInt32(em), Convert.ToInt32(es));
            ba.FTime = new DateTime(ftime.Year, ftime.Month, ftime.Day, Convert.ToInt32(fh), Convert.ToInt32(fm), Convert.ToInt32(fs));
            ba.Lastupdate = DateTime.Now;

            JsonResponse.ResponseObject = DaoFactory.BindAdvertisementDao.Save(ba);
        }

        public void GetBindAd()
        {
            var ID = JsonRequest.GetParam<Guid>("ID");
            JsonResponse.ResponseObject = DaoFactory.BindAdvertisementDao.GetListByEKey(ID);
        }

        //删除广告
        public void DelUpLoadAd()
        {
            var ad = JsonRequest.GetParam<XProject.DataVisit.Model.Advertisement.Advertisement>("gg");
            //删除文件
            if (PublicFn.DelFolder(HttpContext.Current.Server.MapPath("../../") + "/AdvertisementFile/Advertisements/" + ad.FolderName))
            {
                JsonResponse.ResponseObject = DaoFactory.AdvertisementDao.Delete(ad);
            }
            else
            {
                JsonResponse.ResponseObject = null;
            }
        }

        #region 流程流转
        /// <summary>
        /// 获取需要本人审核的广告
        /// </summary>
        public void GetFlowAd()
        {
            JsonResponse.ResponseObject = DaoFactory.AdvertisementDao.GetSHList(CurrentUser.PID);
        }
        /// <summary>
        /// 开始流程
        /// </summary>
        public void BindFlow()
        {
            var info = JsonRequest.GetParam<XProject.DataVisit.Model.Advertisement.Advertisement>("info");
            var flowinfo = JsonRequest.GetParam<FlowName>("flowinfo");
            info.BelongFlow=flowinfo.PID;
            info.TransfState = 2;
            //绑定流程
            DaoFactory.AdvertisementDao.Update(info);
            //初始化流转表
            IList<FlowNode> nodelist = DaoFactory.FlowNodeDao.GetNodeList(flowinfo.PID);
            foreach (var k in nodelist)
            {
                if (k.SortOrder == 1)
                {
                    FlowTransf ft = new FlowTransf();
                    ft.ContentID = info.PID.ToString();
                    ft.FlowID = flowinfo.PID;
                    ft.LastUpdate = DateTime.Now;
                    ft.NodeID = k.PID;
                    ft.PersonID = CurrentUser.PID;
                    DaoFactory.FlowTransfDao.Save(ft);
                }
            }
            JsonResponse.HasError = false;
        }

        //通过
        public void PassFlow()
        {
            var content = JsonRequest.GetParam<string>("c");
            var info = JsonRequest.GetParam<FlowTemp>("info");
            //找到第二个节点
            var k= DaoFactory.FlowNodeDao.SearchPassNode(info);
            if (k != null)
            {
                var currentnode=DaoFactory.FlowTransfDao.GetById(info.FlowPID);
                FlowRecord fr = new FlowRecord();
                fr.ContentID = currentnode.ContentID;
                fr.FlowID = currentnode.FlowID;
                fr.LastUpdate = DateTime.Now;
                fr.NodeID = currentnode.NodeID;
                fr.PersonID = currentnode.PersonID;
                fr.Remark = content;
                DaoFactory.FlowRecordDao.Save(fr);
                DaoFactory.FlowTransfDao.Delete(currentnode);
                //装入新的流程节点
                FlowTransf ft = new FlowTransf();
                ft.FlowID = info.FlowID;
                ft.ContentID = info.ContentID;
                ft.LastUpdate = DateTime.Now;
                ft.NodeID = k.PID;
                ft.PersonID = DaoFactory.UserInfoDao.GetOneData(k.RoleID).PID;
                DaoFactory.FlowTransfDao.Save(ft);
            }
            else
            { //流程结束
                var currentnode = DaoFactory.FlowTransfDao.GetById(info.FlowPID);
                FlowRecord fr = new FlowRecord();
                fr.ContentID = currentnode.ContentID;
                fr.FlowID = currentnode.FlowID;
                fr.LastUpdate = DateTime.Now;
                fr.NodeID = currentnode.NodeID;
                fr.PersonID = currentnode.PersonID;
                fr.Remark = content;
                DaoFactory.FlowRecordDao.Save(fr);
                DaoFactory.FlowTransfDao.Delete(currentnode);
                var i = DaoFactory.AdvertisementDao.GetById(info.PID);
                i.TransfState = 1;
                DaoFactory.AdvertisementDao.Update(i);
            }
            JsonResponse.HasError = false;
        }

        //拒绝
        public void RejectFlow()
        {
            var content = JsonRequest.GetParam<string>("c");
            var info = JsonRequest.GetParam<FlowTemp>("info");

            //找到第二个节点
            var k = DaoFactory.FlowNodeDao.SearchRejectNode(info);
            if (k != null)
            {
                var currentnode = DaoFactory.FlowTransfDao.GetById(info.FlowPID);
                FlowRecord fr = new FlowRecord();
                fr.ContentID = currentnode.ContentID;
                fr.FlowID = currentnode.FlowID;
                fr.LastUpdate = DateTime.Now;
                fr.NodeID = currentnode.NodeID;
                fr.PersonID = currentnode.PersonID;
                fr.Remark = content;
                DaoFactory.FlowRecordDao.Save(fr);
                DaoFactory.FlowTransfDao.Delete(currentnode);
                //装入新的流程节点
                FlowTransf ft = new FlowTransf();
                ft.FlowID = info.FlowID;
                ft.ContentID = info.ContentID;
                ft.LastUpdate = DateTime.Now;
                ft.NodeID = k.PID;
                ft.PersonID = DaoFactory.UserInfoDao.GetOneData(k.RoleID).PID;
                DaoFactory.FlowTransfDao.Save(ft);
            }
            JsonResponse.HasError = false;
        }
        #endregion

        #region 申请单流程流转
        /// <summary>
        /// 获取需要本人审核的申请单
        /// </summary>
        public void GetFlowDoc()
        {
            JsonResponse.ResponseObject = DaoFactory.DocumentInfoDao.GetSHList(CurrentUser.PID);
        }
        /// <summary>
        /// 开始流程
        /// </summary>
        public void BindFlowDoc()
        {
            var info = JsonRequest.GetParam<DocumentInfo>("info");
            var flowinfo = JsonRequest.GetParam<FlowName>("flowinfo");
            info.DocState = 1;
            //绑定流程
            DaoFactory.DocumentInfoDao.Update(info);
            //初始化流转表
            IList<FlowNode> nodelist = DaoFactory.FlowNodeDao.GetNodeList(flowinfo.PID);
            foreach (var k in nodelist)
            {
                if (k.SortOrder == 1)
                {
                    FlowTransf ft = new FlowTransf();
                    ft.ContentID = info.PID.ToString();
                    ft.FlowID = flowinfo.PID;
                    ft.LastUpdate = DateTime.Now;
                    ft.NodeID = k.PID;
                    ft.PersonID = CurrentUser.PID;
                    DaoFactory.FlowTransfDao.Save(ft);
                }
            }
            JsonResponse.HasError = false;
        }

        //通过
        public void PassFlowDoc()
        {
            var content = JsonRequest.GetParam<string>("c");
            var info = JsonRequest.GetParam<DocFlowTemp>("info");
            //找到第二个节点
            var k = DaoFactory.FlowNodeDao.SearchPassNodeDoc(info);
            if (k != null)
            {
                var currentnode = DaoFactory.FlowTransfDao.GetById(info.FlowPID);
                FlowRecord fr = new FlowRecord();
                fr.ContentID = currentnode.ContentID;
                fr.FlowID = currentnode.FlowID;
                fr.LastUpdate = DateTime.Now;
                fr.NodeID = currentnode.NodeID;
                fr.PersonID = currentnode.PersonID;
                fr.Remark = content;
                DaoFactory.FlowRecordDao.Save(fr);
                DaoFactory.FlowTransfDao.Delete(currentnode);
                //装入新的流程节点
                FlowTransf ft = new FlowTransf();
                ft.FlowID = info.FlowID;
                ft.ContentID = info.ContentID;
                ft.LastUpdate = DateTime.Now;
                ft.NodeID = k.PID;
                ft.PersonID = DaoFactory.UserInfoDao.GetOneData(k.RoleID).PID;
                DaoFactory.FlowTransfDao.Save(ft);
            }
            else
            { //流程结束
                var currentnode = DaoFactory.FlowTransfDao.GetById(info.FlowPID);
                FlowRecord fr = new FlowRecord();
                fr.ContentID = currentnode.ContentID;
                fr.FlowID = currentnode.FlowID;
                fr.LastUpdate = DateTime.Now;
                fr.NodeID = currentnode.NodeID;
                fr.PersonID = currentnode.PersonID;
                fr.Remark = content;
                DaoFactory.FlowRecordDao.Save(fr);
                DaoFactory.FlowTransfDao.Delete(currentnode);
                var i = DaoFactory.DocumentInfoDao.GetById(info.PID);
                i.DocState = 2;
                DaoFactory.DocumentInfoDao.Update(i);
            }
            JsonResponse.HasError = false;
        }

        //拒绝
        public void RejectFlowDoc()
        {
            var content = JsonRequest.GetParam<string>("c");
            var info = JsonRequest.GetParam<DocFlowTemp>("info");

            //找到第二个节点
            var k = DaoFactory.FlowNodeDao.SearchRejectNodeDoc(info);
            if (k != null)
            {
                var currentnode = DaoFactory.FlowTransfDao.GetById(info.FlowPID);
                FlowRecord fr = new FlowRecord();
                fr.ContentID = currentnode.ContentID;
                fr.FlowID = currentnode.FlowID;
                fr.LastUpdate = DateTime.Now;
                fr.NodeID = currentnode.NodeID;
                fr.PersonID = currentnode.PersonID;
                fr.Remark = content;
                DaoFactory.FlowRecordDao.Save(fr);
                DaoFactory.FlowTransfDao.Delete(currentnode);
                //装入新的流程节点
                FlowTransf ft = new FlowTransf();
                ft.FlowID = info.FlowID;
                ft.ContentID = info.ContentID;
                ft.LastUpdate = DateTime.Now;
                ft.NodeID = k.PID;
                ft.PersonID = DaoFactory.UserInfoDao.GetOneData(k.RoleID).PID;
                DaoFactory.FlowTransfDao.Save(ft);
            }
            JsonResponse.HasError = false;
        }
        #endregion
    }
}
